package com.tansun.easycare.rule.common.model;

import java.io.Serializable;
import java.text.DecimalFormat;

import com.alibaba.druid.util.StringUtils;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.tansun.easycare.core.config.Global;
import com.tansun.easycare.rule.exception.BizException;


/**
 * 规则模型基础信息
 *
 * @author Lph
 */
public class BaseModel implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 709024010556887943L;
    /**
     * 模型ID
     */
    private String id;
    /**
     * 模型编号
     */
    private String code;
    /**
     * 模型名称
     */
    private String name;
    /**
     * 项目编码
     */
    private String projectCode;
    /**
     * 规则项文件保存路径
     */
    private String itemPath;
    /**
     * 项目状态  0:删除，1：正常 2：发布 3:暂存版本
     */
    private String status;
    /**
     * 当前版本号
     */
    private String version;
    /**
     * 创建人
     */
    private String createdBy;
    /**
     * 时间
     */
    private String createdOn;
    /**
     * 修改人
     */
    private String modifiedBy;
    /**
     * 修改时间
     */
    private String modifiedOn;

    /**
     * 授权人;
     */
    private String users;
    /**
     * 锁定人
     */
    private String locker;
    /**
     * 所有人
     */
    private String owner;
    /**
     * 更新数据是否升级版本 作为开关，本身不要持久化到xml中
     */
    private boolean upgrad = true;
    /**
     * 规则模型类型
     */
    private String modelType;
    private String rankRelationId;
    public String getRankRelationId() {
		return rankRelationId;
	}

	public void setRankRelationId(String rankRelationId) {
		this.rankRelationId = rankRelationId;
	}

	public boolean isUpgrad() {
        return upgrad;
    }

    public void setUpgrad(boolean upgrad) {
        this.upgrad = upgrad;
    }

    public String getUsers() {
        return users;
    }

    public void setUsers(String users) {
        this.users = users;
    }

    public String getLocker() {
        return locker;
    }

    public void setLocker(String locker) {
        this.locker = locker;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public String getCreatedOn() {
        return createdOn;
    }

    public void setCreatedOn(String createdOn) {
        this.createdOn = createdOn;
    }

    public String getModifiedBy() {
        return modifiedBy;
    }

    public void setModifiedBy(String modifiedBy) {
        this.modifiedBy = modifiedBy;
    }

    public String getModifiedOn() {
        return modifiedOn;
    }

    public void setModifiedOn(String modifiedOn) {
        this.modifiedOn = modifiedOn;
    }

    public String getProjectCode() {
        return projectCode;
    }

    public void setProjectCode(String projectCode) {
        this.projectCode = projectCode;
    }

    public String getItemPath() {
        return itemPath;
    }

    public void setItemPath(String itemPath) {
        this.itemPath = itemPath;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getModelType() {
        return modelType;
    }

    public void setModelType(String modelType) {
        this.modelType = modelType;
    }

    /**
     * 获取数据库名称
     */
    @JsonIgnore
    public String getDbName() {
        return Global.getConfig("jdbc.type");
    }

    /**
     * 初始化改条记录的修改内容
     */
    public void InitModifiedContent(BaseModel base) throws BizException {
        if (base == null) base = this;
        if (!base.upgrad) {
            this.users = base.getUsers();
            this.owner = base.getOwner();
            this.locker = base.getLocker();
            return;
        }
        if (!StringUtils.isEmpty(base.getStatus()))
            this.setStatus(base.getStatus());
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        if ("2".equals(base.getStatus())) //发布
        {
            String verStr = this.getVersion().substring(1, this.getVersion().length() - 3);
            float version = Float.parseFloat(verStr);
            if (this.getVersion().matches("v\\d+\\.00")) {
                throw new BizException("已经是发布版本，不能重复发布");
            }
            version = version + 1;
            this.setVersion("v" + decimalFormat.format(version));
        } else if ("1".equals(base.getStatus())) //修改
        {
            String verStr = this.getVersion().substring(1, this.getVersion().length());
            float version = Float.parseFloat(verStr);
            version = (float) (version + 0.01);
            this.setVersion("v" + decimalFormat.format(version));
        }
    }
}
